package www.wxwnb.clerk.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import www.wxwnb.clerk.R;

/**
 * Created by dingguanxu on 2017/8/7.
 */

public abstract class BaseFragment extends Fragment {

    public static final int RETURN_CODE = 1;
    public static final String RETURN_MSG = "登陆成功";

    public static final String JSON_RETURN_CODE = "returnCode";
    public static final String JSON_RETURN_MSG = "returnMsg";

    public static final String TAG = "BaseFragment";

    public Activity mContext;

    private ProgressDialog mDefaultProgressDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        Log.e(TAG, getClass().getSimpleName() );
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initListener();
        initData();
    }

    public abstract View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    public abstract void initListener();

    public abstract void initData();

    public void intentActivity(Context context, Class<?> taretClass) {
        startActivity(new Intent(context, taretClass));
    }

    public void intentActivity(Context context, Bundle bundle, Class<?> taretClass) {
        Intent intent = new Intent(context, taretClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 修改文字 下划线 + 颜色
     *
     * @param text
     * @param front
     * @param behind
     * @return
     */
    public SpannableStringBuilder modifyProtocol(String text, int front, int behind) {
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        ForegroundColorSpan blueSpan = new ForegroundColorSpan(Build.VERSION.SDK_INT <= 23 ? getResources().getColor(R.color.colorMain) : getResources().getColor(R.color.colorMain, null));
        UnderlineSpan lineSpan = new UnderlineSpan();
//        builder.setSpan(lineSpan, 8, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //下划线
        builder.setSpan(blueSpan, front, behind, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //字体颜色
        return builder;
    }

    /**
     * 获取当前系统时间
     *
     * @param format
     * @return
     */
    public String getDate(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date curDate = new Date(System.currentTimeMillis());
        String time = simpleDateFormat.format(curDate);
        return time;
    }

    public String getDateString(String text) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parseDate = null;
        try {
            parseDate = simpleDateFormat.parse(text);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = parseDate.getTime();
        return String.valueOf(time);
    }

    /**
     * px = density * dp
     *
     * @param px
     * @return
     */
    public int px2Dp(int px) {
        float density = getActivity().getResources().getDisplayMetrics().density;
        int dp = (int) (px / density + 0.5f);
        return dp;
    }

    public int dp2Px(int dp) {
        float density = getActivity().getResources().getDisplayMetrics().density;
        int px = (int) (dp * density + 0.5f);
        return px;
    }

    public void showDefaultProgressDialog(String title, String msg) {
        mDefaultProgressDialog = ProgressDialog.show(this.getActivity(), title, msg, false, false);
    }

    public void showDefaultProgressDialog(String title, String msg, boolean indeterminate, boolean cancelable) {
        mDefaultProgressDialog = ProgressDialog.show(this.getActivity(), title, msg, indeterminate, cancelable);
    }

    public void dismissDefaultProgressDialog() {
        mDefaultProgressDialog.dismiss();
    }

    public void backgroundAlpha(float alp) {
        WindowManager.LayoutParams attributes = getActivity().getWindow().getAttributes();
        attributes.alpha = alp;
        getActivity().getWindow().setAttributes(attributes);
    }
}
